//页面初始化加载方法
efform_onload = function ()
{ 
	$('#ef_region_detail').hide();
};  

/********************************************************************************
 ***********************************主表操作************************************* 
 ********************************************************************************/
//主表查询
button_query_onclick = function ()
{
	efgrid.submitInqu( "ef_grid_r", "ZM","ZMCP50","query");
}
//主表修改
button_update_onclick = function (){ 
	if (isSelected("ef_grid_r") == 0)return; 
	efgrid.submitForm( "ef_grid_r", "ZM","ZMCP50","update",true);
}
//主表新增
button_insert_onclick = function (){
	if (isSelected("ef_grid_r") == 0)return; 
	efgrid.submitForm( "ef_grid_r", "ZM","ZMCP50","insert",true);
}
//主表删除
button_delete_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="草拟")
		{
			EFAlert("只能对草拟的记录进行删除！"); return;
		}
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM","ZMCP50","delete",true);
	});
}
//主表上报
button_report_onclick = function (){  
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	if(rows.length > 1) {
		EFAlert("一次只能上报一条记录！"); return;
	}
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")=="上报")
		{
			EFAlert("已经上报的记录，不能再次上报！"); return;
		}
	}
	EFConfirm('你确认要上报吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM","ZMCP50","report",true);
	});
}
//主表单击行操作
efgrid_onRowClicked = function( grid_id, row_index ){
	if(grid_id == "ef_grid_r"){ 
		var grid = efgrid.getGridObject(grid_id);
		var fdProfessionalplanId=grid.getCellValueByColumnName(row_index,"fdKeyId");
		var fdPlanYear=grid.getCellValueByColumnName(row_index,"fdPlanYear");
		var fdPlanMonth=grid.getCellValueByColumnName(row_index,"fdPlanMonth");
		var fdPlanStatus=grid.getCellValueByColumnName(row_index,"fdPlanStatus");
		//隐藏、显示从表按钮
		if(fdPlanStatus!='草拟')
			hideAndShowSubButtons(false);
		else
			hideAndShowSubButtons(true);
		var info = new EiInfo();
		info.set("fdProfessionalplanId",fdProfessionalplanId); 
		info.set("fdPlanYear",fdPlanYear); 
		info.set("fdPlanMonth",fdPlanMonth); 
		$('#fdProfessionalplanId').val(fdProfessionalplanId);
		$('#fdPlanYear').val(fdPlanYear);
		$('#fdPlanMonth').val(fdPlanMonth);
		EiCommunicator.send("ZMCP51", "query", info, ajax_callback_getdetail);  
	}
}
//主表单击行回调函数
var ajax_callback_getdetail= {
	onSuccess: function(eiInfo) { 
		 efwindow.hide();
		 var grid = efgrid.getGridObject("ef_grid_d");
		 grid.refresh(eiInfo);
		 $('#ef_region_detail').show();
	},
	onFail:function(eMsg)
	{
		EFAlert("新增失败，原因："+eMsg); 
	}
}

/********************************************************************************
 ***********************************从表操作************************************* 
 ********************************************************************************/
//从表新增
button_insertdetail_onclick = function (){
	if (isSelected("ef_grid_d") == 0)return; 
	efgrid.submitForm( "ef_grid_d", "ZM","ZMCP51","insert",true);
};
//从表修改
button_updatedetail_onclick = function (){ 
	if (isSelected("ef_grid_d") == 0)return; 
	efgrid.submitForm( "ef_grid_d", "ZM","ZMCP51","update",true);
};
//从表删除
button_deletedetail_onclick = function (){
	var grid=isSelected("ef_grid_d");
	if (grid == 0)return;  
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r){
			efgrid.submitForm( "ef_grid_d", "ZM","ZMCP51","delete",true);
		}
	});
}
/********************************************************************************
 ***********************************公共方法**************************************** 
 ********************************************************************************/
//根据数据状态显示或掩藏按钮函数
var hideAndShowSubButtons=function(status)
{
	if(!status)
	{
		efbutton.setButtonStatus("insertdetail", false);
		efbutton.setButtonStatus("updatedetail", false);
		efbutton.setButtonStatus("deletedetail", false);
	}else
	{
		efbutton.setButtonStatus("insertdetail", true);
		efbutton.setButtonStatus("updatedetail", true);
		efbutton.setButtonStatus("deletedetail", true);
	}
}
//新增行后的回调函数
efgrid_afterAddNewRow = function(grid_id){
	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows(); 
	if(grid_id=="ef_grid_r")
	{
		var fdCreator=$('#fdCreator').val();
		var fdCreateDepartment=$('#fdCreateDepartment').val();
	    var now = new Date();
	    var year = now.getFullYear();       //年
	    var month = now.getMonth() + 1;     //月
	    if(month > 12) {
	    	month = 1;
	    	year += 1;
	    }
	    if(month < 10) month = "0"+month;
	    var day = now.getDate();            //日 
	    var clock = year + "-" + month + "-";
	    if(day < 10) clock += "0";
	    clock += day;
		for(var i = 0 ; i < rows.length;i++){
			grid.setCellValue(rows[i],0,"草拟",TYPE_DATA); 
			grid.setCellValue(rows[i],1,year,TYPE_DATA);
			grid.setCellValue(rows[i],2,month,TYPE_DATA);
			grid.setCellValue(rows[i],3,"建造粉尘计划",TYPE_DATA);
			grid.setCellValue(rows[i],4,year+"年"+month+"月的建造粉尘计划",TYPE_DATA);
			grid.setCellValue(rows[i],5,fdCreator,TYPE_DATA);
			grid.setCellValue(rows[i],6,fdCreateDepartment,TYPE_DATA); 
			grid.setCellValue(rows[i],7,clock,TYPE_DATA);
		}
	}
}

//新增、修改、删除后的执行方法
efgrid_onAjaxSubmitSuccess = function(gridId, service_name, method_name, eiInfo) {
	if(gridId == "ef_grid_d") {
		if (eiInfo.status != -1) {
			var grid = efgrid.getGridObject("ef_grid_d");
			grid.refresh( eiInfo );
		}
		else
		{
			submitCallBackEiMsg = eiInfo.msg;
		}
	} else {
		var grid = efgrid.getGridObject(gridId);
		grid.refresh( eiInfo );
		clearRegion("ef_grid_d");
		$("#ef_region_detail").hide();
	}
};